<?php

class Order_model extends CI_Model {

    function __construct() {
        parent::__construct();

        $this->tableName = "order_main"; //
    }

    /*
     * 添加
     */

    public function addData($data) {
        $outArr = array();
        if (!empty($data)) {
            $outArr = $this->db->insert($this->tableName, $data);
            $outArr = $this->db->insert_id();
        }
        return $outArr;
    }

    /*
     * 编辑
     */

    public function updateData($paramArr = array()) {
        $optionArr = array(
            "fieldArr" => array(),
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        if (!empty($fieldArr)) {
            $outArr = $this->db->update($this->tableName, $fieldArr, $whereArr);
        }
        return $outArr;
    }

    /*
     * 删除
     */

    public function delData($paramArr = array()) {
        $optionArr = array(
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        $outArr = $this->db->delete($this->tableName, $whereArr);
        return $outArr;
    }

    /*
     * 得到
     */

    public function getData($where = '', $page = 1, $pageCount = 20, $orderBy = "ORDER BY id DESC", $fields = '*') {

        $result = array();
        $sql = "SELECT " . $fields . " FROM " . $this->tableName . " " . $where . " " . $orderBy;
        if ($pageCount > 0) {
            $sql .= " LIMIT " . (($page - 1) * $pageCount) . ", " . $pageCount;
        }
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    /*
     * 得到条数
     */

    function getDataCount($where = '') {
        $num = 0;
        $sql = "SELECT COUNT(*) AS num FROM " . $this->tableName . " " . $where . " LIMIT 1";
        $query = $this->db->query($sql);
        if ($query) {
            $res = $query->result_array();
            $num = $res[0]["num"];
        }
        return $num;
    }

    public function getDataById($id) {
        $outArr = array();
        $outArr = $this->getData("WHERE id=" . $id, 1, 0, "");
        if (!empty($outArr)) {
            $outArr = $outArr[0];
        }
        return $outArr;
    }

    function status() {
        return [
            "created" => ["id" => "created", "name" => "已制单", "time_tag" => "制单时间", "sort_id" => 1],
            "pending_vehicle" => ["id" => "pending_vehicle", "name" => "已派车", "time_tag" => "派车时间", "sort_id" => 2],
            "setting_off" => ["id" => "setting_off", "name" => "已出发", "time_tag" => "发车时间", "sort_id" => 3],
            "arrived" => ["id" => "arrived", "name" => "到提货", "time_tag" => "到提货点时间", "sort_id" => 4],
            "loading" => ["id" => "loading", "name" => "装载中", "time_tag" => "开始装载时间", "sort_id" => 5],
            "delivering" => ["id" => "delivering", "name" => "已装载", "time_tag" => "装载完成时间", "sort_id" => 6],
            "at_delivery" => ["id" => "at_delivery", "name" => "到送货", "time_tag" => "到达送货点时间", "sort_id" => 7],
            "off_loading" => ["id" => "off_loading", "name" => "卸货中", "time_tag" => "开始卸货时间", "sort_id" => 8],
            "completed" => ["id" => "completed", "name" => "已完成", "time_tag" => "卸货完成时间", "sort_id" => 9],
        ];
    }

    public function needField($addParam = []) {
        $param = [
            ["field" => "type_id", "name" => "订单类型", "is_need" => 1, "type" => "z_int", "form_type" => "select"],
            ["field" => "order_date", "name" => "订单日期", "is_need" => 1, "type" => "date_time",],
            ["field" => "order_no", "name" => "订单号", "is_need" => 1, "type" => "string"],
            ["field" => "set_day", "name" => "订单设天", "is_need" => 1, "type" => "z_int"],
            ["field" => "set_id", "name" => "订单设ID", "is_need" => 1, "type" => "z_int"],
            ["field" => "kehu_custom_id", "name" => "客户", "is_need" => 1, "type" => "z_int", "form_type" => "select"],
            ["field" => "org_id", "name" => "提货地", "is_need" => 0, "type" => "int"],
            ["field" => "org_province_id", "name" => "提货地省份", "is_need" => 0, "type" => "int"],
            ["field" => "org_city_id", "name" => "提货地州", "is_need" => 0, "type" => "int"],
            ["field" => "org_district_id", "name" => "提货地区县", "is_need" => 0, "type" => "int"],
            ["field" => "org_address", "name" => "提货地具体地址", "is_need" => 0, "type" => "string"],
            ["field" => "dest_id", "name" => "送货地", "is_need" => 0, "type" => "int"],
            ["field" => "dest_province_id", "name" => "送货地省份", "is_need" => 0, "type" => "int"],
            ["field" => "dest_city_id", "name" => "送货地城市", "is_need" => 0, "type" => "int"],
            ["field" => "dest_district_id", "name" => "送货地区县", "is_need" => 0, "type" => "int"],
            ["field" => "dest_address", "name" => "送货地具体地址", "is_need" => 0, "type" => "string"],
            ["field" => "goods_name", "name" => "货物名称", "is_need" => 0, "type" => "string"],
            ["field" => "num", "name" => "件数", "is_need" => 0, "type" => "float"],
            ["field" => "weight", "name" => "重量(KG)", "is_need" => 0, "type" => "float"],
            ["field" => "xianghao", "name" => "箱号1", "is_need" => 0, "type" => "string"],
            ["field" => "xianghao2", "name" => "箱号2", "is_need" => 0, "type" => "string"],
            ["field" => "remark", "name" => "备注/要求", "is_need" => 0, "type" => "string"],
        ];
        if (!empty($addParam)) {
            $param = array_merge($param, $addParam);
        }
        return $param;
    }

    public function paicheNeedField($addParam = []) {
        $param = [
            ["field" => "pending_vehicle_at", "name" => "派车日期", "is_need" => 1, "type" => "date_time",],
            ["field" => "gongyingshang_custom_id", "name" => "供应商", "is_need" => 1, "type" => "z_int", "form_type" => "select"],
            ["field" => "car_id", "name" => "车辆", "is_need" => 1, "type" => "z_int", "form_type" => "select"],
        ];
        if (!empty($addParam)) {
            $param = array_merge($param, $addParam);
        }
        return $param;
    }

    function type() {
        return [
            1 => ["name" => "散货"],
            2 => ["name" => "出口"],
            3 => ["name" => "进口"],
        ];
    }

    function secondType() {
        return [
            1 => ["name" => "提重"],
            2 => ["name" => "还空"],
            3 => ["name" => "提空"],
            4 => ["name" => "还重"],
            5 => ["name" => "二次"],
        ];
    }

    function isHuicheng() {
        return [
            1 => ["name" => "已安排"],
            0 => ["name" => "未安排"],
        ];
    }

    function completeStatus() {
        return [
            3 => ["name" => "已制单"],
            1 => ["name" => "已完成"],
            2 => ["name" => "未完成"],
        ];
    }

    function hexiaoStatus() {
        return [
            1 => ["name" => "已核销"],
            0 => ["name" => "未核销"],
        ];
    }

}
